package com.s10.ecloud.web.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.s10.ecloud.web.model.SystemUser;
import com.s10.ecloud.web.model.IndividualUser;
import com.s10.ecloud.web.service.IUserService;


public class UserAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	private int id;
	private String username;
	private String password;
	private String passwordOld;
	private String name;
	private String gender;
	private String dep;
	private String phone;
	private String pid;
	private String email;
	private String right;
	
	

	private IUserService userManager;
	
	
	public IUserService getUserManager() {
		return userManager;
	}
	@Resource
	public void setUserManager(IUserService userManager) {
		this.userManager = userManager;
	}
	public String getUsername() {
		return username;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getRight() {
		return right;
	}
	public void setRight(String name) {
		this.right = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getDep() {
		return dep;
	}
	public void setDep(String dep) {
		this.dep = dep;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getPid() {
		return pid;
	}
	public void setPid(String pid) {
		this.pid = pid;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String execute() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		//response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=utf-8");    
		SystemUser user=new SystemUser();
		IndividualUser info=new IndividualUser();
		
		user.setName(username);
		user.setPassword(password);
		
		user.setRole(null);
//		if(right.equals("2")) 
//			user.setState(1);
//		else 
			
			user.setState(0);
		
		info.setEmail(email);
		info.setName(name);
		info.setGender(gender);
		//////System.out.println(gender);
		info.setPersonalId(pid);
		info.setPhone(phone);
//		info.setUser(user);
		
		PrintWriter out = response.getWriter();
		int res=userManager.userRegister(user, info, Integer.parseInt(dep));
		
		
		if(res == 2){
			
			out.print("<script>alert('用户已经存在!');"
					+ " window.location.href='../usermanage/register.jsp'; //"+  user.getId() + "//</script>");
		}
		
		if(res == 0){
			if(right.equals("2")) out.print("<script>alert('用户注册成功！');"
					+ " window.location.href='../login.jsp'; //"+  user.getId() + "//</script>");
			else out.print("<script>alert('用户注册成功！请等待管理员审核！');"
					+ " window.location.href='../login.jsp';//"+ user.getId() + "//</script>");
		}else{
			
			out.print("<script>alert('注册用户失败！');"
					+ " window.location.href='../usermanage/register.jsp'</script>");
			
		}
		return null;
	}
	
	public String findUserinfo(){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		
		SystemUser user=(SystemUser) request.getSession(true).getAttribute("user");
		
		IndividualUser info=userManager.findUserinfo(user);
		
		request.setAttribute("userinfo", info);
		
		
		return SUCCESS;
		
	}
	
	public String modifyUserinfo(){
		HttpServletRequest request = ServletActionContext.getRequest();
		
		SystemUser user=(SystemUser) request.getSession(true).getAttribute("user");
			
		userManager.modifyUserinfo(name,gender,email,pid,phone,user);
		
		return SUCCESS;
	}
	
	public String findAllUsers(){
		HttpServletRequest request = ServletActionContext.getRequest();
					
		List<SystemUser> list=userManager.findAllUser();
		
		request.setAttribute("users", list);
		
		return SUCCESS;
	}
	
	public String findAllUsersByDep(){
		HttpServletRequest request = ServletActionContext.getRequest();
			
		SystemUser user=(SystemUser) request.getSession().getAttribute("user");
		
		List<SystemUser> list=userManager.findAllUserByDep(user);
		
		request.setAttribute("users", list);
		
		return SUCCESS;
	}
	
	public String findAllUsersByRight(){
		HttpServletRequest request = ServletActionContext.getRequest();
			
		SystemUser user=(SystemUser) request.getSession().getAttribute("user");
		
		List<SystemUser> list=userManager.findAllUserByRight(right);
		
		request.setAttribute("users", list);
		
		return SUCCESS;
	}
	
	public String freezeState(){
	     
		userManager.freezeUserstateByUsername(username);
		
		return SUCCESS;
	}
	
	public String recoverState(){
		
		userManager.recoverUserstateByUsername(username);
		
		return SUCCESS;
	}
	
	public String modifyRights(){
		
		userManager.modifyUserRights(username,right);
		
		return SUCCESS;
	}
	
	//�?����������String���͵�id  ����struts2���Զ�ת����int��
	public String modifyUserPwd() throws IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = response.getWriter();
		SystemUser user=(SystemUser) request.getSession(true).getAttribute("user");
		////System.out.println();
		if(user.getPassword().equals(passwordOld)){
		    String res=userManager.modifyUserPwd(id, password);
		
		    if(res.equals("修改密码成功!")){
			
			      out.print("<script>alert(' 修改密码成功!');"
					+ " window.location.href='../frames/body.jsp'</script>");
			
		     }else{
			
			      out.print("<script>alert('修改密码失败！');"
					+ " window.location.href='../frames/body.jsp'</script>");
			
		     }
		} else 
			out.print("<script>alert('原密码错误！');"
					+ " window.location.href='../frames/body.jsp'</script>");
		return null;
	}
	public String getPasswordOld() {
		return passwordOld;
	}
	public void setPasswordOld(String passwordOld) {
		this.passwordOld = passwordOld;
	}

}
